﻿using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HuiZe.education.View
{
    /// <summary>
    /// MyShareResource.xaml 的交互逻辑
    /// </summary>
    public partial class RecycleBinView : UserControl
    {
        AttendClassViewModel attendClassViewModel { get; set; }
        public RecycleBinView()
        {
            InitializeComponent();
            Loaded += AccountLoginView_Loaded;
        }

        private void AccountLoginView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            attendClassViewModel = DataContext as AttendClassViewModel;

            updata(0, 1);
            //pageManage.ItemSourceCount = planLessonView.PageCount;
        }


        private void updata(int type, int pages)
        {
            ImageList.Children.Clear();
            var list = attendClassViewModel.GetResourcesdellist(1);
            foreach (var item in list)
            {
                RecycleBinControl recycleBinControl = new RecycleBinControl();
                recycleBinControl.Width = 1200;
                recycleBinControl.Margin = new Thickness(5, 10, 0, 0);
                recycleBinControl.bookName = item.title;
                recycleBinControl.createTime = item.createtime_text.ToString();
                recycleBinControl.Id = item.id.ToString();
                recycleBinControl.Chk_Book.Click += Chk_Book_Click;
                //mybkBookEdit5 book = new mybkBookEdit5();
                //book.NomalImage = item.image_cover;
                //book.bookName = item.title;
                //book.shareName = item.nickname;
                //book.createTime = item.createtime_text.ToString();
                //book.Id = item.id.ToString();
                //book.Margin = new Thickness(10, 10, 0, 0);
                //book.Height = 250;
                //book.Width = 200;
                //book.DownLoadUrl = item.zippath;
                //book.DelImg.MouseLeftButtonDown += DelImg_MouseDown;

                ImageList.Children.Add(recycleBinControl);


            }

            //pageManage.ItemSourceCount = attendClassViewModel.RecycleBinPageCount;
        }

        private void Chk_Book_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;


            FrameworkElement __do = sender as FrameworkElement;
            RecycleBinControl bookbtn = new RecycleBinControl();

            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.RecycleBinControl")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as RecycleBinControl;
                    //ParentView.Update0();
                    break;
                }


                if (__do.Parent == null) break;
            }

            if (checkBox.IsChecked == true)
            {
              
                if (!string.IsNullOrWhiteSpace(bookbtn.Id))
                {

                    if (!attendClassViewModel.RecycleDelIdList.Contains(bookbtn.Id))
                        attendClassViewModel.RecycleDelIdList.Add(bookbtn.Id);
                }
            }

            else
            {

                if (!string.IsNullOrWhiteSpace(bookbtn.Id))
                {

                    if (attendClassViewModel.RecycleDelIdList.Contains(bookbtn.Id))
                        attendClassViewModel.RecycleDelIdList.Remove(bookbtn.Id);
                }

            }
        }

        private void DelImg_MouseDown(object sender, MouseButtonEventArgs e)
        {

            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            var button = sender;
            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn = new mybkBookEdit();
            mybkBookEdit5 bookbtn2 = new mybkBookEdit5();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit5")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit5;
                    //ParentView.Update0();
                    break;
                }

                if (__do.Parent == null) break;
            }

            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                //mainView.DelBclass(Convert.ToInt32(bookbtn.Id));
                updata(0, 1);
            }

            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                //mainView.DelBclass(Convert.ToInt32(bookbtn2.Id));
                updata(0, 1);
            }
        }


        private void PageManage_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            //if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            //{
            //    CustomRadioButton button = e.OriginalSource as CustomRadioButton;
            //    string index = button.Content.ToString();
            //    if (index == ">")
            //    {
            //        //index = pageManage.Content.ToString();
            //        index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
            //    }
            //    if (index == "<")
            //    {
            //        index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
            //    }
            //    if (index == "首页")
            //    {
            //        index = "1";
            //    }
            //    if (index == "尾页")
            //    {
            //        index = pageManage.ItemSourceCount.ToString();
            //    }


            //    updata(0, Convert.ToInt32(index));


            //}
        }
        //GetResourcesdelBack()


        /// <summary>
        /// 一键清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            attendClassViewModel.SetResourcesdelall();
            updata(0, 1);
        }

        private void CustomButton_Click(object sender, RoutedEventArgs e)
        {
            attendClassViewModel.GetResourcesdelBack();
            updata(0, 1);
        }
    }
}
